/**
 * 产品分类列表
 * 文件命名格式：zmn.模块名称.表名称.功能.jas
 * @author heguanghua
 * @since 2018/10/18
 */
layui.define(['index', 'form', 'table', 'zmnApp', 'laypage', 'layedit', 'laydate', 'upload'], function (exports) {
    var $ = layui.$;
    var form = layui.form;
    var layer = layui.layer;
    var table = layui.table;
    var laypage = layui.laypage;
    let upload = layui.upload;
    let zmnApp = layui.zmnApp;

    importExcel();

    $(document).on('click', '#outPutTemplate', function () {
        let index = layer.load(1, {
            shade: [0.5, '#DBDBDB']
        });

        let templetUrl = "https://img1.xiujiadian.com/prod/common/员工绩效模板.xlsx";
        let downUrl = zmnApp.SYS_BASE_URL +"/common/download.action?urlFile=" +templetUrl;
        zmnUtil.download(downUrl,{}, "员工绩效模板.xlsx",function () {
            layer.close(index);
        });
    });

    function importExcel() {
        let loadingExcel;
        upload.render({
            elem: '#importExcel'
            , multiple: false
            , accept: 'file'
            , exts: "xlsx|xls"
            , url: "/staff/importExcel.action"
            , before: function (obj) {
                loadingExcel = layui.layer.load(1, {
                    shade: [0.5, '#DBDBDB']
                });
            }
            , done: function (res) {
                if (res.status == 200) {
                    // layer.msg(res.data, {icon: 1});
                    let index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                    // parent.layer.close(index);
                    if (res.data != null && res.data.length > 0) {
                        layer.open({
                            type: 1
                            ,title: "导入员工绩效"
                            ,offset: "auto"
                            ,content: '<div style="padding: 10px 20px;">'+ res.message +'</div>'
                            ,btn: '下载错误名单'
                            ,btnAlign: 'c' //按钮居中
                            ,shade: 0 //不显示遮罩
                            ,yes: function(){
                                let url = zmnApp.SYS_BASE_URL +"/staff/outPutErrorData.action";
                                let xhr = new XMLHttpRequest();
                                xhr.open('post', url, true);
                                xhr.responseType = 'blob';
                                xhr.setRequestHeader('Content-Type', 'application/json;charset=utf-8');
                                xhr.onload = function () {
                                    if (this.status == 200) {
                                        var blob = this.response;
                                        var a = document.createElement('a');
                                        var url = window.URL.createObjectURL(blob);
                                        a.href = url;
                                        //设置文件名称
                                        a.download = "错误名单.xlsx";
                                        a.click();
                                    }
                                }
                                xhr.send(JSON.stringify(res.data));
                            }
                        });
                    } else {
                        layer.open({
                            type: 1
                            ,title: "导入员工绩效"
                            ,offset: "auto"
                            ,content: '<div style="padding: 10px 20px;">'+ res.message +'</div>'
                            ,btn: '确认'
                            ,btnAlign: 'c' //按钮居中
                            ,shade: 0 //不显示遮罩
                            ,yes: function(){
                                layer.closeAll();
                            }
                        });
                    }
                } else {
                    layer.msg(res.message, {icon: 2});
                }
                layui.layer.close(loadingExcel);
            }
            , error: function (resErr) {
                layui.layer.close(loadingExcel);
            }
        });
    }

    //对外暴露的接口
    exports('staff_import_performance', {});

});
